﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.Win.Controls;
using PlanningGIS.ManageUtility;
using PlanningGIS.Util.Convertor;

namespace PlanningGIS.Manager
{
    /// <summary>
    /// 日志管理控件
    /// </summary>
    public partial class CtlLog : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public CtlLog()
        {
            InitializeComponent();

            cboType.Items.Add("<所有类型>");

            DataTable dt = ManageHelper.DBContext.GetUniqueValue("LOG", "OP_TYPE");
            if (dt.Rows.Count >= 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    cboType.Items.Add(dr[0].ToString());
                }
            }
            cboType.SelectedIndex = 0;
        }

        private BillCommands _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public BillCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    BillCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }

        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldCmd">Old property value</param>
        /// <param name="newCmd">New property value</param>
        protected virtual void OnCommandsChanged(BillCommands oldCmd, BillCommands newCmd)
        {
            btnCancel.Command = newCmd != null ? newCmd.LogCommands.Cancel : null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "select * from LOG where ISDELETED = 0";
                string begin = "";
                string end = "";
                if (dtiBegin.ValueObject != null && dtiEnd.ValueObject != null && dtiBegin.Value >= dtiEnd.Value)
                {
                    MsgHelper.ShowWarningMsgbox("起始时间不能晚于结束时间。");
                    return;
                }

                if (dtiBegin.ValueObject != null)
                {
                    begin = "OP_TIME  >= to_date('" + dtiBegin.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd') ";
                }

                if (dtiEnd.ValueObject != null)
                {
                    end = "OP_TIME  <= to_date('" + dtiEnd.Value.ToString("yyyy-MM-dd") + "','yyyy-mm-dd') ";
                }

                if (begin != "" || end != "")
                {
                    string where = " and ";
                    if (begin != "" && end != "")
                    {
                        where += (begin + " and " + end);
                    }
                    else
                    {
                        where += (begin + end);
                    }

                    sql += where;
                }

                if (cboType.SelectedIndex > 0)
                {
                    sql += (" and OP_TYPE = '" + cboType.Text + "'");
                }

                sql += " order by OP_TIME desc ";
                DataTable dt = ManageHelper.DBContext.GetDataTable(sql);

                dgvLog.Rows.Clear();
                foreach (DataRow dr in dt.Rows)
                {
                    string id = StringHelper.ConvertWRTNull(dr["ID"]);
                    string userName = StringHelper.ConvertWRTNull(dr["USER_NAME"]);
                    string ip = StringHelper.ConvertWRTNull(dr["IP"]);
                    string opType = StringHelper.ConvertWRTNull(dr["OP_TYPE"]);
                    string data = StringHelper.ConvertWRTNull(dr["DATA_INVOLOVE"]);
                    string notes = StringHelper.ConvertWRTNull(dr["NOTES"]);
                    string time = StringHelper.ConvertWRTNull(dr["OP_TIME"]);
                    string status = StringHelper.ConvertWRTNull(dr["OP_STATUS"]);

                    int idx = dgvLog.Rows.Add();
                    DataGridViewRow dgvr = dgvLog.Rows[idx];

                    dgvr.Cells["clnUserName"].Value = userName;
                    dgvr.Cells["clnIP"].Value = ip;
                    dgvr.Cells["clnType"].Value = opType;
                    dgvr.Cells["clnData"].Value = data;
                    dgvr.Cells["clnData"].ToolTipText = data;
                    dgvr.Cells["clnTime"].Value = time;
                    dgvr.Cells["clnStatus"].Value = status;
                    dgvr.Cells[clnDesp.Index].Value = notes;
                    dgvr.Tag = id;

                }
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox("查询日志失败:" + ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvLog.SelectedRows.Count == 0) return;

            if (MsgHelper.ShowYesOrNoQueryMsgBox("您确定要删除选中的日志信息？") == DialogResult.No) return;

            try
            {
                Dictionary<string, dynamic> values = new Dictionary<string, dynamic>();
                values.Add("ISDELETED", 1);

                foreach (DataGridViewRow dgvr in dgvLog.SelectedRows)
                {
                    string id = StringHelper.ConvertWRTNull(dgvr.Tag);
                    Dictionary<string, dynamic> wheres = new Dictionary<string, dynamic>();
                    wheres.Add("ID", id);

                    ManageHelper.DBContext.Update("LOG", values, wheres);

                    dgvLog.Rows.Remove(dgvr);

                }
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox("删除日志失败:" + ex.Message);
            }
        }


    }
}
